// Bhumika Udani       v1.2               dt: 25/01/2011
//  bug fixed for Opprty Split owner id 
// Manish Tiwari      v1.3               dt: 27/09/2011
// Manish Tiwari      v1.4              dt: 25/11/2011
// Updated Logic as per R 729

trigger UpdateOptySplits on Opportunity (after insert, after update) {

    List<Opportunity_Split__c> newSplitList = new list<Opportunity_Split__c>();
    List<Opportunity_Split__c> updateSplitList = new list<Opportunity_Split__c>();
    List<Opportunity_Split__c> deleteSplitList = new list<Opportunity_Split__c>();

    static ID localAdvertiserLeadRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Lead Order');
    static ID localAdvertiserRenewalRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');

    if(trigger.isInsert){
        for(Opportunity opty : trigger.new){
                       
                if(opty.recordtypeid == localAdvertiserLeadRecordTypeID || opty.recordtypeid == localAdvertiserRenewalRecordTypeID){
             // Start  :- Added  for resolve Active inactive Owner Issue[SZ] 

               if(opty.Opportunity_Type__c=='Digital Express' || opty.Opportunity_Type__c=='Bill Cycle Evergreen' ||opty.Opportunity_Type__c=='Net Revenue'
                ||opty.Opportunity_Type__c=='AF Assigned')
               {
                
                 if(opty.Ext_Secondary_Owner__c != null){        
                        
                    NewSplitList.add(OpptySplitSharingClass.CreateSplit(opty, 50, 'Primary', opty.Ext_Primary_Owner__c));
                    NewSplitList.add(OpptySplitSharingClass.CreateSplit(opty, 50, 'Secondary', opty.Ext_Secondary_Owner__c));
                }
                else{
                    NewSplitList.add(OpptySplitSharingClass.CreateSplit(opty, 100, 'Primary', opty.Ext_Primary_Owner__c));
                } 
                system.debug('NewSplitList ' + NewSplitList.size());
             
               }else {   
                  // End  :- Added  for resolve Active inactive Owner Issue[SZ]
                //This logic logic is for other opportunity          
                if(opty.secondary_owner__c != null){        
                    NewSplitList.add(OpptySplitSharingClass.CreateSplit(opty, 50, 'Primary', opty.OwnerId));
                    NewSplitList.add(OpptySplitSharingClass.CreateSplit(opty, 50, 'Secondary', opty.secondary_owner__c));
                }
                else{
                    NewSplitList.add(OpptySplitSharingClass.CreateSplit(opty, 100, 'Primary', opty.OwnerId));
                } 
                  // Start  :- Added  for resolve Active inactive Owner Issue[SZ]
               }
                   // End  :- Added  for resolve Active inactive Owner Issue[SZ]            
            }
        }
        
       if (NewSplitList.size() > 0)
        insert NewSplitList;
    }               
    else if(trigger.isUpdate){
        // Create a map of opportunity IDs to existing split IDs
        List<id> optyIds = new list<id>();
        Map<id, List<Opportunity_Split__c>> optyToSplits = new Map<id, List<Opportunity_Split__c>>();
        for(opportunity opty : trigger.new){
            if(opty.recordtypeid == localAdvertiserLeadRecordTypeID || opty.recordtypeid == localAdvertiserRenewalRecordTypeID){
                optyIds.add(opty.id);              
            }
        }
           
        if(optyIds.size() > 0){
            List<Opportunity_Split__c> existingSplits;
            existingSplits = [select id, Opportunity__c from Opportunity_Split__c where opportunity__c in :optyIds order by name asc];

            for(Opportunity_Split__c split : existingSplits){
                if(!optyToSplits.containsKey(split.opportunity__c))
                    optyToSplits.put(split.opportunity__c, new List<Opportunity_Split__c>());

                optyToSplits.get(split.opportunity__c).add(split);
            }           
        }

        for(opportunity opty : trigger.new){
           
           if(opty.recordtypeid == localAdvertiserLeadRecordTypeID || opty.recordtypeid == localAdvertiserRenewalRecordTypeID){
     
            List<Opportunity_Split__c> recycleSplits = optyToSplits.get(opty.id);
                // Start  :- Added  for resolve Active inactive Owner Issue[SZ] 
               if(opty.Opportunity_Type__c=='Digital Express' || opty.Opportunity_Type__c=='Bill Cycle Evergreen' || opty.Opportunity_Type__c=='Net Revenue' 
               ||opty.Opportunity_Type__c=='AF Assigned')
               {
                    if(opty.Ext_Secondary_Owner__c != null){        
                    OpptySplitSharingClass.CreateOrUpdateSplit(opty, 50, 'Primary', opty.Ext_Primary_Owner__c, recycleSplits, newSplitList, updateSplitList);
                    OpptySplitSharingClass.CreateOrUpdateSplit(opty, 50, 'Secondary', opty.Ext_Secondary_Owner__c, recycleSplits, newSplitList, updateSplitList);
                    }else{
                     OpptySplitSharingClass.CreateOrUpdateSplit(opty, 100, 'Primary', opty.Ext_Primary_Owner__c, recycleSplits, newSplitList, updateSplitList);
                    }
                
                
               }else{
             // End  :- Added  for resolve Active inactive Owner Issue[SZ] 


            if(opty.secondary_owner__c != null){        
                OpptySplitSharingClass.CreateOrUpdateSplit(opty, 50, 'Primary', opty.OwnerId, recycleSplits, newSplitList, updateSplitList);
                OpptySplitSharingClass.CreateOrUpdateSplit(opty, 50, 'Secondary', opty.secondary_owner__c, recycleSplits, newSplitList, updateSplitList);
            }
            else{
                OpptySplitSharingClass.CreateOrUpdateSplit(opty, 100, 'Primary', opty.OwnerId, recycleSplits, newSplitList, updateSplitList);
            }
             // Start  :- Added  for resolve Active inactive Owner Issue[SZ] 
               }
           // End  :- Added  for resolve Active inactive Owner Issue[SZ] 
            
            if(recycleSplits != null && recycleSplits.size() > 0){
                deleteSplitList.addall(recycleSplits);
            }
          }  
        }
        
        if(newSplitList.Size() > 0){
            insert newSplitList;
        }
        if(updateSplitList.Size() > 0){
            update updateSplitList;
        }
        if(deleteSplitList.Size() > 0){
            delete deleteSplitList;
        }

    }
}